package com.extrace.ui.domain;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.extrace.loader.ExpressLoader;
import com.extrace.misc.model.ExpressSheet;
import com.extrace.net.IDataAdapter;
import com.extrace.ui.main.R;
import com.zxing.util.CaptureActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ExpressPackageListActivity extends AppCompatActivity implements IDataAdapter<ExpressSheet> {

    public static final int REQUEST_CAPTURE_PACKING = 100;
    public static final int REQUEST_CAPTURE_ExPACKING = 101;

    private int method;//选择快件两种方法
    private TextView chooseExpress;
    private ListView expressLv;
    private String pkgID;
    private Button btnFinish;
    private Button btnDestroy;
    private List<ExpressSheet> list;
    private List<ExpressSheet> listIsSelected;//被选择的快件列表

    private ExpressListAdapter1 mAdapter;
    private Intent mIntent;
    private ExpressLoader mLoader;
    private ExpressSheet mItem;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.express_package_list);

        expressLv = (ListView)findViewById(R.id.expresslist);
        list = new ArrayList<ExpressSheet>();
        listIsSelected = new ArrayList<ExpressSheet>();
        mAdapter = new ExpressListAdapter1(list, this);
        expressLv.setAdapter(mAdapter);


        chooseExpress = (TextView)findViewById(R.id.choose_express);
        chooseExpress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartCapture(method);
            }
        });

        expressLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ExpressListAdapter1.ViewHolder holder = (ExpressListAdapter1.ViewHolder) view.getTag();

                holder.cb.toggle();
                ExpressListAdapter1.getIsSelected().put(position, holder.cb.isChecked());
                if(holder.cb.isChecked()) {
                    listIsSelected.add(mAdapter.getItem(position));
                } else {
                    listIsSelected.remove(mAdapter.getItem(position));
                }
            }
        });
        btnFinish = (Button)findViewById(R.id.btnfinish);
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("ExpressSheet", (Serializable)listIsSelected);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        btnDestroy = (Button)findViewById(R.id.btnDestroy);
        btnDestroy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExPackaging();
                //RefreshListByPackage(pkgID);
            }
        });

        mIntent = getIntent();
        if (mIntent.hasExtra("Action")) {
            if (mIntent.getStringExtra("Action").equals("Packaging")) {
                //RefreshList("");
                StartCapture(REQUEST_CAPTURE_PACKING);
                DisplayBtn(1);
            } else if (mIntent.getStringExtra("Action").equals("ExPackaging")) {
                pkgID = mIntent.getStringExtra("PackageID");
                //RefreshListByPackage(pkgID);
                StartCapture(REQUEST_CAPTURE_ExPACKING);
                DisplayBtn(2);
            } else {
                setResult(RESULT_CANCELED, mIntent);
                finish();
            }
        } else {
            setResult(RESULT_CANCELED, mIntent);
            finish();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (resultCode) {
            case RESULT_OK:
                switch (requestCode) {
                    case REQUEST_CAPTURE_PACKING:
                        if(data.hasExtra("BarCode")) {
                            String id = data.getStringExtra("BarCode");
                            try {
                                mLoader = new ExpressLoader(this, this);
                                mLoader.GetExpressWeatherInPackage(id);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                        break;
                    case REQUEST_CAPTURE_ExPACKING:
                        if(data.hasExtra("BarCode")) {
                            String id = data.getStringExtra("BarCode");
                            try {
                                mLoader = new ExpressLoader(this, this);
                                mLoader.GetExpressWeatherInPackage1(id, pkgID);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                        break;

                }
        }

    }

    private void ExPackaging() {
        for (ExpressSheet es : listIsSelected) {
            try {
                mLoader = new ExpressLoader(this, this);
                mLoader.MoveExpressFromPackage(es.getID(), pkgID);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        finish();
    }

    private void DisplayBtn(int display_code) {
        switch (display_code) {
            case 1:
                btnFinish.setVisibility(View.VISIBLE);
                btnDestroy.setVisibility(View.INVISIBLE);
                method = REQUEST_CAPTURE_PACKING;
                break;
            case 2:
                btnFinish.setVisibility(View.INVISIBLE);
                btnDestroy.setVisibility(View.VISIBLE);
                method = REQUEST_CAPTURE_ExPACKING;
                break;
        }
    }

    private void StartCapture(int code){
        Intent intent = new Intent();
        intent.putExtra("Action","Captrue");
        intent.setClass(this, CaptureActivity.class);
        startActivityForResult(intent, code);
    }

    @Override
    public ExpressSheet getData() {
        return mItem;
    }

    @Override
    public void setData(ExpressSheet data) {
        mItem = data;
    }

    @Override
    public void notifyDataSetChanged() {
        list.remove(mItem);
        list.add(mItem);
        mAdapter.setData(list);
        mAdapter.notifyDataSetChanged();
    }
}
